global projectdir "~"
global datadir "$projectdir/data"


************************************************************

cd $datadir/raw_pulls/umetrics/FSRDC_2018
use um_iris_uer_2018_remp_rsch, clear

keep empnumber cfda proportionearningsallocated

egen cfda_num = group(cfda)

collapse (sum) proportionearningsallocated, by(empnumber cfda_num) fast

* To break ties in sort
set seed 54613
gen double rand1 = runiform()
gen double rand2 = runiform()
gen double rand3 = runiform()

gsort empnumber -proportionearningsallocated rand1 rand2 rand3

by empnumber: gen rank = _n
keep if rank == 1

rename cfda_num modal_cfda

keep empnumber modal_cfda

sort empnumber
rename empnumber iris_employee_number
compress
cd $datadir/intermediate_files
save cfda_clusters, replace

************************************************************
